<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true" label-width="70px">
        <SearchFormGroup
          :data="searchParam"
          :hasGageEquipCode="false"
          @change-search="handleSearchChange"
          @change-gageEquipCode="changeEquipCode"
        />
        <el-form-item label="工单状态">
          <el-select
            v-model="searchParam.workOrderStatus"
            placeholder="请选择工单状态"
            clearable
            @change="changeStatus"
          >
            <el-option
              v-for="item in useDictionary('PsmOrderStatus')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工单时间">
          <el-date-picker
            v-model="searchParam.collectionTimeSearch"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD HH:mm:ss"
            @change="getcollectionTime"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="reset">
            <i-ep-refresh />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <!--      <template #header>-->
      <!--        <el-button type="success" plain @click="openDialog()"><i-ep-plus />新增</el-button>-->
      <!--                -->
      <!--      </template>-->

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        :maxHeight="tableHeight"
      >
        <el-table-column
          label="工单状态"
          prop="workOrderStatus"
          min-width="80"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{
              useDictionary("PsmOrderStatus", scope.row.workOrderStatus) || ""
            }}
          </template>
        </el-table-column>
        <el-table-column
          label="工单生成时间"
          prop="createTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="最近一次处理时间"
          prop="lastUpdateTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="工段"
          prop="workshopSectionCode"
          min-width="80"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="工序"
          prop="processCode"
          min-width="80"
          show-overflow-tooltip
          align="center"
        />
        <!-- <el-table-column
          label="生产线"
          prop="processEquipCode"
          min-width="80"
          show-overflow-tooltip
          align="center"
        /> -->
        <el-table-column
          label="生产线"
          prop="equipName"
          min-width="110"
          show-overflow-tooltip
          align="center"
        />
        <!-- <el-table-column
          label="项目描述"
          prop="projectName"
          min-width="110"
          show-overflow-tooltip
          align="center"
        /> -->
        <el-table-column
          label="型号描述"
          prop="modelName"
          min-width="110"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="产品型号"
          prop="modelCode"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="监控分类"
          prop="abilityType"
          min-width="120"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{ useDictionary("AbilityTypeMin", scope.row.abilityType) }}
          </template>
        </el-table-column>
        <el-table-column
          label="特性名称"
          prop="paramName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="监控描述"
          prop="workInfo"
          min-width="130"
          show-overflow-tooltip
        />
        <el-table-column fixed="right" label="操作" width="80" align="center">
          <template #default="scope">
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="查看"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="openDialogdetail(scope.row)"
              >
                <i-ep-search />
              </el-button>
            </el-tooltip>
            <el-tooltip
              :hide-after="0"
              effect="light"
              content="处理"
              placement="top"
            >
              <el-button
                type="primary"
                size="small"
                link
                @click="openDialog(scope.row)"
              >
                <i-ep-edit />
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <pcmWorkOrders-edit
      :key="dayjsValue"
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import {
  useTable,
  useDownload,
  useDictionary,
  useComputeTableHeight,
} from "@/utils/useTable";
import request from "@/utils/request";
import {
  getMdmProcessEquipApi,
  getMdmprocessListApi,
  getMdmproductinfosApi1,
  getMdmProcessProjectApi,
} from "@/utils/commonData";
import dayjs from "dayjs";
import SearchFormGroup from "@/views/pcm/pcmMonitoring/components/SearchFormGroup1.vue";
import _ from "lodash-es";

defineOptions({
  name: "PCMPCMWORKORDERS",
  inheritAttrs: false,
});

// 处理传回数据
const handleSearchChange = (data) => {
  Object.assign(searchParam.value, data);
  // 可执行字段相关的特定逻辑
};

// 请求参数
const requestApi = "/dmpDosPcm/pcmworkorder/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  // 工段
  workshopSectionId: "",
  workshopSectionCode: "",
  // 工序
  processCode: "",
  processId: "",
  // 设备
  equipCode: "",
  equipId: "",
  // 产品型号
  productCode: "",
  productId: "",
  // 检具类型
  gageEquipToolKind: "",
  // 检具名称
  gageEquipCode: "",

  modelCode: "",

  workOrderStatus: "",
  //   programName: "",
  collectionTimeSearch: [],
  collectionTimeStart: "",
  collectionTimeEnd: "",
};

const deleteParam = {
  api: "/dmpDosPcm/pcmworkorder/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const tableHeight = ref();

function dataCallBack(data: any) {
  setTimeout(() => {
    tableHeight.value = useComputeTableHeight();
  }, 100);
  return data;
}

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack,
  requestError,
  deleteParam
);

onMounted(async () => {
  getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/dmpDosPcm/pcmworkorder/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

const dayjsValue = ref(dayjs().valueOf());
/** 打开工单管理表单弹窗 */
function openDialog(roleObj?: Object) {
  dayjsValue.value = dayjs().valueOf();
  if (roleObj) {
    addDialog.title = "修改工单管理";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增工单管理";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开工单管理表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "工单管理详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function getcollectionTime(data: any) {
  if (data) {
    searchParam.value.collectionTimeStart = data[0];
    searchParam.value.collectionTimeEnd = data[1];
  } else {
    searchParam.value.collectionTimeStart = "";
    searchParam.value.collectionTimeEnd = "";
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { data } = await request({
      url: `/dmpDosPcm/pcmworkorder/deleteItem/${id}`,
      method: "post",
    });
    search();
  });
}

// 间距变化
const changeEquipCode = (id: string) => {
  if (id) {
  }
};

function changeStatus(id) {
  console.log("changeStatus", JSON.parse(JSON.stringify(searchParam.value)));
}
watch(
  () => searchParam.value,
  (newVal) => {
    searchParam.value.modelCode = newVal.productCode;
  },
  {
    deep: true,
  }
);
</script>
